﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace UI.Admin.Content.ModelArticle
{
    /// <summary>
    /// AjaxAction 的摘要说明
    /// </summary>
    public class AjaxAction : IHttpHandler, IRequiresSessionState
    {

        public void ProcessRequest(HttpContext context)
        {
            // 验证
           // string auth = Utility.Authorization.Check(HttpContext.Current.Session["role"], "ContentAdmin", context.Request["ColumnID"]);
          //  if (auth != "ACCEPT")
           // {
           //     return;
           // }

            context.Response.ContentType = "text/plain";

            string dowhat = context.Request["dowhat"];
            if (dowhat == null) dowhat = "";

            // 操作用户及IP
            string UserName = ((Models.User)(HttpContext.Current.Session["user"])).Name;
            string UserIP = context.Request.ServerVariables["LOCAl_ADDR"];

            #region 添加文章模型的内容
            if (dowhat == "add_article")
            {
                string ColumnID = context.Request["ColumnID"];

          
                string FirstEditor = UserName;
                string LastEditor = UserName;

                string Title = context.Request["Title"];
                string Content = context.Request["Content"].ToString();
                string Source = context.Request["Source"];
                string Status = context.Request["Status"];
                string UpdateDate = DateTime.Now.ToString();

                string Name = BLL.Find.FindOneColumn(ColumnID).Name;

                if (BLL.Create.InsertArticle(ColumnID, FirstEditor, LastEditor, Title, Content, UpdateDate, Source, Status) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "添加文章模型栏目【" + Name + "】" + Title + "内容");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("faild");
                }
                context.Response.End();
                return;
            }
            #endregion

            #region 修改文章模型的内容
            if (dowhat == "update_article")
            {
                string ID = context.Request["ID"];
                string ColumnID = context.Request["ColumnID"];

                // 修改最后编辑者
                string LastEditor = UserName;

                string Title = context.Request["Title"];
                string Content = context.Request["Content"];
                string Source = context.Request["Source"];
                string Status = context.Request["Status"];
                string UpdateDate = DateTime.Now.ToString();

                string Name = BLL.Find.FindOneColumn(ColumnID).Name;

                if (BLL.Update.UpdateArticle(ID, ColumnID, LastEditor, Title, Content, UpdateDate, Source, Status) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "修改文章模型栏目【" + Name + "】" + Title + "内容");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("faild");
                }
                context.Response.End();
                return;
            }
            #endregion

            #region 删除文章模型的内容
            if (dowhat == "delete_article")
            {
                string ID = context.Request["ID"];

                string Title = BLL.Find.FindArticle(ID).Title;
                string Name = BLL.Find.FindOneColumn(context.Request["ColumnID"]).Name;

                if (BLL.Delete.DeleteArticle(ID) > 0)
                {
                    // Log
                    BLL.Create.CreateLogRecord(UserName, UserIP, "删除文章模型栏目【" + Name + "】" + Title + "内容");
                    context.Response.Write("success");
                }
                else
                {
                    // Log
                    context.Response.Write("failed");
                }
                context.Response.End();
                return;
            }
            #endregion
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}